package com.hls.service;

import com.hls.enmus.ResultEnum;
import com.hls.exception.BoyException;
import com.hls.repository.BoyRepository;
import com.hls.domain.Boy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * Created by huangliusong on 2017/7/21.
 */
@Service
public class BoyService {
    @Autowired
    private BoyRepository boyRepository;
    //添加事物注解
    @Transactional
    public void insertBoyTow(){
        Boy boy1=new Boy();
        boy1.setAge("67");
        boy1.setBirth("1967");
        Boy boy2=new Boy();
        boy2.setAge("688");
        boy2.setBirth("1968");

        boyRepository.save(boy1);
        boyRepository.save(boy2);
    }
    public void getAge(Integer id) throws  Exception{
        Boy boy=boyRepository.findOne(id);
        Integer age=Integer.parseInt(boy.getAge());
        System.out.println(age);
        if(age<10){
            //返回你还在上小学
            throw new BoyException(ResultEnum.PRIMARY_SCHOOL);
        }else if(age>10&&age<16){
            //返回 你还在上初中
            throw new BoyException(ResultEnum.MIDDLE_SCHOOL);
        }

    }

    /**
     * 通过id查询详细信息
     * @param id
     * @return
     */
    public Boy findOneBoy(Integer id){
        return boyRepository.findOne(id);
    }
}
